﻿@{
    ViewBag.Title = "AddRights";
    Layout = "~/Views/Shared/_CommonLayout.cshtml";
}
<script src="~/Scripts/jquery.hashchange.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.easytabs.js" type="text/javascript"></script>
<link type="text/css" href="~/Content/themes/easytabs/easytabs.css" rel="stylesheet"/>

<div id="tab-container" class="tab-container addrights">
    <input type="hidden" id="vehicleRights" value="@ViewBag.vehicleRightsJson" />
    <input type="hidden" id="menuRights" value="@ViewBag.menuRightsJson" />
    <input type="hidden" id="userId" value="@ViewBag.userId" />
    <ul class='etabs'>
        <li class='tab'><a href="#tab_vehicle">车辆单位权限</a></li>
        <li class='tab'><a href="#tab_menu">菜单权限</a></li>
    </ul>
    <div class="panel-container">
        <div id="tab_vehicle" class="tabitem">
            <div class="left">
                <ul id="vehicleTree" class="ztree"></ul>
            </div>
            <div class="middle">
            </div>
            <div class="right">
                <ul id="selectVehicleTree" class="ztree"></ul>
            </div>
            <div class="clear"></div>
        </div>
        <div id="tab_menu" class="tabitem">
            <div class="left">
                <ul id="menuTree" class="ztree"></ul>
            </div>
            <div class="middle">

            </div>
            <div class="right">
                <ul id="selectMenuTree" class="ztree"></ul>
            </div>
            <div class="clear"></div>
        </div>
    </div>
    <div id="AtTopRight">
        <div id="MultiSelectDiv">
            <input type="checkbox" id="MultiSelect" checked="checked" onclick="MultiSelect(this)" />级联选择
        </div>
        <input type="button" id="btnAllocateRights" value="分配车辆单位权限" onclick="AllocateRights()" />
    </div>
</div>
<div>

</div>
<script type="text/javascript">
    $("#MultiSelectDiv").hide();
    $(".etabs a").click(function () {//点击tab时，改变按钮的标题
        if ($(this).text() == "车辆单位权限") {
            $("#MultiSelectDiv").hide();
        }
        else {
            $("#MultiSelectDiv").hide();
        }
        $("#btnAllocateRights").val("分配" + $(this).text());
    });

    $('#tab-container').easytabs();//形成tab切换视图

    var setting = {
        check: {
            enable: true,
            chkboxType: { "Y": "ps", "N": "ps" }/*2014-07-28添加*/
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onCheck: OnCheck
        }
    };
    var setting2 = {
        check: {
            enable: true,
            chkboxType: { "Y": "ps", "N": "ps" }/*2014-07-31添加*/
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onCheck: OnCheck
        }
    };
    var selectsetting = {
        data: {
            simpleData: {
                enable: true
            }
        },
    };
    function OnCheck(event, treeid, treeNode) {
        if (treeid == "vehicleTree") {
            OnVehicleCheck(treeNode);
        }
        else if (treeid == "menuTree") {
            OnMenuCheck();
        }
    }

    //车辆单位权限
    var vehicleNodes = $.parseJSON($("#vehicleRights").val());
    //初始化初始数据的展开状态
    function ResetVehicleNodes() {
        for (var j = 0; j < vehicleNodes.length; j++) {
            vehicleNodes[j].open = false;
        }
    }
    //保存节点的展开状态
    function SaveVehicleNodes() {
        var selectZtree = $.fn.zTree.getZTreeObj("selectVehicleTree");
        var nodes = selectZtree.transformToArray(selectZtree.getNodes());
        for (var j = 0; j < vehicleNodes.length; j++) {
            for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].id == vehicleNodes[j].id) {
                    vehicleNodes[j].open = nodes[i].open;
                    break;
                }
            }
        }
    }
    ExpandOneLevel(vehicleNodes);
    $.fn.zTree.init($("#vehicleTree"), setting, vehicleNodes);

    //选择的车辆单位
    var selectVehicleNodesArray = [];
    $.fn.zTree.init($("#selectVehicleTree"), selectsetting, selectVehicleNodesArray);

    //添加权限时，在权限分配页面右边树形增加或者删除节点
    function OnVehicleCheck(treeNode) {
        SaveVehicleNodes();
        selectVehicleNodesArray = [];
        var ztree = $.fn.zTree.getZTreeObj("vehicleTree");
        var selectZtree = $.fn.zTree.getZTreeObj("selectVehicleTree");
        var nodes = ztree.getCheckedNodes(true);
        for (var i = 0; i < nodes.length; i++) {
            nodes[i].others = "";
        }
        var rootIndex;
        if (nodes != null && nodes.length != 0) {
            //找到根节点
            for (var i = 0; i < nodes.length; i++) {
                var findRoot = true;
                for (var j = 0; j < nodes.length; j++) {
                    if (nodes[i].pId == nodes[j].id) {
                        findRoot = false;
                        break;
                    }
                }
                if (findRoot == true) {
                    rootIndex = i;
                    break;
                }
            }
            FilterNodes(ztree, nodes[rootIndex]);
            for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].others == "no") {
                    continue;
                }
                for (var j = 0; j < vehicleNodes.length; j++) {
                    if (nodes[i].id == vehicleNodes[j].id) {
                        //半选状态为部分权限 ，否则为全部权限 j
                        var checkStatus = nodes[i].getCheckStatus();
                        if (checkStatus['half'] == true && checkStatus['checked'] == true) {
                            vehicleNodes[j].checked = false;
                        }
                        else {
                            vehicleNodes[j].checked = true;
                        }
                        selectVehicleNodesArray.push(vehicleNodes[j]);
                        break;
                    }
                }
            }
        }
        else {
            ResetVehicleNodes();
        }
        ExpandOneLevel(selectVehicleNodesArray);
        $.fn.zTree.init($("#selectVehicleTree"), selectsetting, selectVehicleNodesArray);
    }
    function FilterNodes(tree, rootnode) {
        var nodes = rootnode.children;
        var selectVehicleCount = 0;
        var selectDepartCount = 0;
        var selectDepartIndex = 0;
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].checked == true) {
                if (nodes[i].id.substring(0, 4) == "VEHI") {
                    selectVehicleCount++;
                }
                else {
                    selectDepartCount++;
                    selectDepartIndex = i;
                }
            }
        }
        if (selectDepartCount == 1 && selectVehicleCount == 0) {
            rootnode.others = "no";
            FilterNodes(tree, nodes[selectDepartIndex]);
        }
        else {//有多个子节点，或者只有一个车辆自己点
            return;
        }
    }

    //是否级联选择和取消
    function MultiSelect(object) {
        var zTree = $.fn.zTree.getZTreeObj("vehicleTree");
        if ($(object).attr("checked") == "checked") {
            zTree.setting.check.chkboxType = { "Y": "s", "N": "s" };
        }
        else {
            zTree.setting.check.chkboxType = { "Y": "", "N": "" };
        }
    }
    //菜单权限
    var menuNodes = $.parseJSON($("#menuRights").val());
    var selectMenuNodesArray;
    ExpandOneLevel(menuNodes);
    $.fn.zTree.init($("#menuTree"), setting2, menuNodes);
    function OnMenuCheck(event, treeid, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("menuTree");
        var selectnodes = zTree.getCheckedNodes(true);
        selectMenuNodesArray = [];
        for (var i = 0; i < selectnodes.length; i++) {
            for (var j = 0; j < menuNodes.length; j++) {
                if (selectnodes[i].id == menuNodes[j].id) {
                    selectMenuNodesArray.push(menuNodes[j]);
                    break;
                }
            }
        }
        ExpandOneLevel(selectMenuNodesArray);
        $.fn.zTree.init($("#selectMenuTree"), selectsetting, selectMenuNodesArray);
    }

    //展开一级
    function ExpandOneLevel(rightArray) {
        for (var i = 0; i < rightArray.length; i++) {
            var hasFind = false;
            for (var j = 0; j < rightArray.length; j++) {
                if (rightArray[i].pId == rightArray[j].id) {
                    hasFind = true;
                    break;
                }
            }
            if (!hasFind) {
                rightArray[i].open = true;
                break;
            }
            //if (rightArray[i].pId == null) {
            //    rightArray[i].open = true;
            //}
            //if (rightArray.getParentNode == null) {
            //    rightArray[i].open = true;
            //}
        }
    }

    //已有的单位车辆权限
    function InitVehicleRights() {
        var ztree = $.fn.zTree.getZTreeObj("vehicleTree");
        var nodes = ztree.getCheckedNodes(true);
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].id.substring(0, 4) != "VEHI") {
                CheckParent(nodes[i]);
            }
        }
        OnVehicleCheck(null);
    }
    function CheckParent(node) {
        var ztree = $.fn.zTree.getZTreeObj("vehicleTree");
        var parentnode = node.getParentNode();
        if (parentnode != null && parentnode.checked != true) {
            ztree.checkNode(parentnode, true, false, false);
            CheckParent(parentnode);
        }
    }
    InitVehicleRights();

    //已有的菜单权限
    OnMenuCheck();


    //分配权限
    function AllocateRights() {
        var btnTitle = $("#btnAllocateRights").val();
        artDialogConfirm("确认" + btnTitle + "吗？", function () {
            var type = 0;
            var data = null;
            if (btnTitle == "分配车辆单位权限") {
                type = 1;

                data = selectVehicleNodesArray;
            }
            else if (btnTitle == "分配菜单权限") {
                type = 2;
                data = selectMenuNodesArray;
            }
            else {//分配指令权限
                type = 3;
            }
            $.ajax({
                type: "post",
                url: "/Rights/SubmitRights",
                data: {
                    rightsJson: JSON.stringify(data),
                    userId: $("#userId").val(),
                    operType: type
                },
                dataType: "json",
                success: function (ret) {
                    if (ret.success) {
                        artDialogAlert("权限分配成功！");
                    }
                    else {
                        artDialogAlert("权限分配失败！");
                    }
                }
            });
        }, null);
    }
</script>
